<?php
/**
 * lertaLight - A smart ContentManagementSystem
 * --
 * 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * --
 *
 * @package 	lertaLight
 * @author 		Frederic Darmstädter <fredericdarmstaedter@gmail.com>
 * @copyright 	2010 Frederic Darmstädter <fredericdarmstaedter@gmail.com>
 * @license 	http://opensource.org/licenses/mit-license.php MIT License
 * @version 	2.0 (2010-01-16)
 */
	class cache{
		
		public function getPage($pageRequest){
			$page = new page();
			$file = new file();	
			$config = new config();
			$routes = $config->routes();
			$configuration = $config->config();
			$template = $config->templates();
			
			// Get Templatename
			if(! empty($routes[$pageRequest]['template']))
				$templateName = $routes[$pageRequest]['template'];
			else
				$templateName = $configuration['standard_template'];
			
			// Get Templatefolder and file
			$templateFolder = $template[$templateName]['folder'];
			$templateFile = $template[$templateName]['file'];
			
			
			if(! is_dir('library'))
				$file->createDir('library');
			
			if(! is_dir('library/'. $templateFolder)){
				$filefunc = new file();
				$filefunc->createDir('library/'. $templateFolder);
				$this->copyTemplateData('lertaLight/template/'.$templateFolder.'/library','.',$templateFolder);
			}
			
			if($this->cachedFileExists($pageRequest) AND $this->cacheEnabled()){
					return $file->getContentOfFile('lertaLight/temp/' . md5($pageRequest) . '.temp');
			}
			else{
				$file->createDir('lertaLight/temp');
				$pageContent = $page->getPage($pageRequest);
				$file->createFile('lertaLight/temp/' . md5($pageRequest) . '.temp', $pageContent);
				return $pageContent;
			}		
		}
		
		public function cacheEnabled(){
			$config = new config();
			$configuration = $config->config();
			
			if($configuration['cache'])
				return true;
			return false;
		}
		
		protected function cachedFileExists($pageRequest){
			$pageRequest = md5($pageRequest);
			if(file_exists('lertaLight/temp/' . $pageRequest . '.temp'))
				return true;
			return false;
		}
		
		protected function copyTemplateData($root,$subfolder,$templateFolder){
			// Get Content of the Files
			$create = new file();
			$handler = opendir($root.'/'.$subfolder);
			while ($file = readdir($handler)) {
				if ($file != '.' && $file != '..' && $file != '.DS_Store'){
					if(is_dir($root.'/'.$file)){
						$create->createDir('library/'.$templateFolder.'/'.$subfolder.'/'.$file);
						$this->copyTemplateData($root, $subfolder.'/'.$file,$templateFolder);			
					}
					else{
						$filecontent = fopen($root.'/'.$subfolder.'/'.$file, 'r');
						$filecontentcontent = file_get_contents($root.'/'.$subfolder.'/'.$file);
						fclose($filecontent);
						
						$filename = explode(".", $file);
						if($filename[count($filename) -1] == 'css'){
							// Minify CSS
							file_put_contents('library/'.$templateFolder.'/'.$subfolder.'/'.$file, cssmin::minify($filecontentcontent));
						}
						else{
							$create->createFile('library/'.$templateFolder.'/'.$subfolder.'/'.$file, $filecontentcontent);
						}
						
					}
				}
			}
			closedir($handler);
		}
	}